package demo.practice.competition;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class P5642 {


    public int countPairs(int[] deliciousness) {
        long answer = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        int length = deliciousness.length;
        for (int i = 0; i < length; i++) {
            Integer orDefault = map.getOrDefault(deliciousness[i], 0);
            // 统计数据
            map.put(deliciousness[i], ++orDefault);
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            for (int i = 0; i <= 21; i++) {
                int sum = 1 << i;
                long L = sum - key;
                if (key == L) {//如果是自己，则是从个数中组合2个数据，因为后续要除2，所以计算组合的时候不除
                    answer += 1L * value * (value - 1);
                } else {
                    if (L >= 0 && map.containsKey((int) L)) {
                        answer += 1L * value * map.get((int) L);
                    }
                }
            }
        }
        //结果都算了2遍
        return (int) (answer / 2 % 1000000007);
    }


}
